package com.cxria.problem.utils

import com.cxria.problem.dto.Status
import com.cxria.problem.enums.StatusCodeEnum
import com.cxria.problem.exception.ProblemException
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component

@Component
class ErrorUtils {

    @Autowired
    ObjectMapper objectMapper

    void throwException(Status status){
        String message = objectMapper.writeValueAsString(status)
        throw new ProblemException(message)
    }

    void throwException(Exception e){
        Status status = new Status(Status.ERROR, StatusCodeEnum.UNKNOWN_ERROR, e.message)
        String message = objectMapper.writeValueAsString(status)
        throw new ProblemException(message)
    }

}
